package com.lichuang.fruitvarietysm.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.fruitvarietysm.user.entity.SecurityQuestion;
import com.lichuang.fruitvarietysm.user.entity.User;
import com.lichuang.fruitvarietysm.user.finalproperties.AccountCode;
import com.lichuang.fruitvarietysm.user.mapper.SecurityQuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 密保问题表 前端控制器
 * </p>
 *
 * @author shuaihang
 * @since 2019-06-06
 */
@RestController
@RequestMapping("//security-question")
public class SecurityQuestionController {

    @Autowired
    private UserController userController;

    @Autowired
    private SecurityQuestionMapper securityQuestionMapper;

    /**
     * 添加密保问题
     *
     * @param securityQuestion
     * @return
     */
    @PostMapping("/addSecurityQuestion")
    public Map addSecurityQuestion(@CookieValue(value = "loginToken") String loginToken, SecurityQuestion securityQuestion) {
        Map<String, String> map = new HashMap<>();
        User user = userController.getCurrentUser(loginToken);
        if (user == null) {
            // 没有登录
            map.put("code", AccountCode.UNLOGIN);
            map.put("message", "没有登录");
            return map;
        }
        securityQuestion.setUserId(user.getUserId());
        securityQuestion.setCreateTime(LocalDateTime.now());
        int num = securityQuestionMapper.insert(securityQuestion);
        if (num != 1) {
            map.put("code", AccountCode.NO);
            map.put("message", "插入失败");
            return map;
        }
        map.put("code", AccountCode.OK);
        map.put("message", "添加密保问题成功");
        return map;
    }

    /**
     * 查询用户的密保问题
     *
     * @param loginToken
     * @return
     */
    public Map selectUserSecuriyQuestion(@CookieValue(value = "loginToken") String loginToken) {
        Map<String, Object> map = new HashMap<>();
        User user = userController.getCurrentUser(loginToken);
        if (user == null) {
            // 没有登录
            map.put("code", AccountCode.UNLOGIN);
            map.put("message", "没有登录");
            return map;
        }
        List<SecurityQuestion> list = securityQuestionMapper.selectUserSecurityQuestion(user.getUserId());
        for (SecurityQuestion question : list) {
            question.setQuestionAnswer("");
        }
        map.put("code", AccountCode.OK);
        map.put("message", "查询用户密保问题");
        map.put("securityquestion", list);
        return map;

    }

    /**
     * 验证用户的密保问题
     * @param answer1
     * @param question1
     * @param answer2
     * @param question2
     * @param answer3
     * @param question3
     * @return
     */
    @PostMapping("/validateUserSecuriyQuestion")
    public Map validateUserSecuriyQuestion(@CookieValue(value = "loginToken") String loginToken, String answer1, Integer question1, String answer2, Integer question2, String answer3, Integer question3) {
        Map<String, Object> map = new HashMap<>();
        User user = userController.getCurrentUser(loginToken);
        if (user == null) {
            // 没有登录
            map.put("code", AccountCode.UNLOGIN);
            map.put("message", "没有登录");
            return map;
        }
        SecurityQuestion securityQuestion = new SecurityQuestion();
        securityQuestion.setUserId(user.getUserId());
        securityQuestion.setQuestionId(question1);
        securityQuestion.setQuestionAnswer(answer1);
        QueryWrapper queryWrapper = new QueryWrapper(securityQuestion);
        // 根据用户id，问题id，问题答案来查找是否有存在的数据
        Integer count1 = securityQuestionMapper.selectCount(queryWrapper);

        securityQuestion.setQuestionId(question2);
        securityQuestion.setQuestionAnswer(answer2);
        Integer count2 = securityQuestionMapper.selectCount(queryWrapper);

        securityQuestion.setQuestionId(question3);
        securityQuestion.setQuestionAnswer(answer3);
        Integer count3 = securityQuestionMapper.selectCount(queryWrapper);
        map.put("code",AccountCode.OK);
        map.put("message","答案正确");
        // answer1代表问题一，false代表此问题回答错误
        if (count1 < 1) {
            map.put("code", AccountCode.NO);
            map.put("message","答案错误");
            map.put("answer1", false);
        }
        if (count2 < 1) {
            map.put("code", AccountCode.NO);
            map.put("message","答案错误");
            map.put("answer2", false);
        }
        if (count3 < 1) {
            map.put("code", AccountCode.NO);
            map.put("message","答案错误");
            map.put("answer3", false);
        }
        return map;
    }


}
